package.loaded["TSLib"] = nil
package.loaded["sz"] = nil
require "TSLib"--使用本函数库必须在脚本开头引用并将文件放到设备 lua 目录下
local sz = require("sz")
local plist = sz.plist
--先行变量
function beforeBianLiang()
    nowTime = os.time();
end
-- 读取plist
function loadPlist(muLu, jian1, jian2)
    -- 读取键值
    local plfilename = muLu -- 设置plist路径
    local tmp = plist.read(plfilename) -- 读取plist文件内容并返回一个TABLE
    if tmp == nil then
        fi = io.open(muLu, 'w')
        if fi then
            fi:write(var.plistsTMP)
            fi:close()
            tmp = plist.read(plfilename) -- 读取plist文件内容并返回一个TABLE
        else
            if jian2 ~= nil then
                dialog(muLu .. "_" .. jian1 .. "_" .. jian2, 0)
            else
                dialog(muLu .. "_" .. jian1, 0)
            end
        end
    end
    if jian2 == nil then
        return tmp[jian1]
    else
        return tmp[jian1][jian2]
    end

end
-- 取一个数的整数部分
function getIntPart(x)
    if x <= 0 then
        return math.ceil(x);
    end

    if math.ceil(x) == x then
        x = math.ceil(x);
    else
        x = math.ceil(x) - 1;
    end
    return x;
end

--点击
function touchClick(x, y, ...)
    local cs = math.random(1, 9);
    touchDown(cs, x, y);
    mSleep(300 + cs * 10);
    touchUp(cs, x, y);
    mSleep(1000 + cs * 10);
    -- tiaoShi(x..","..y)
end
-- 自定义滑动
function touchMoveXY(x1, y1, x2, y2)
    touchDown(1, x1, y1)
    mSleep(100)
    local dx = x2 - x1
    local dy = y2 - y1
    dx = getIntPart(dx / 10)
    dy = getIntPart(dy / 10)
    for i = 1, 10 do
        touchMove(1, x1 + dx * i, y1 + dy * i)
        mSleep(50)
    end
    touchMove(1, x2, y2)
    mSleep(50)
    touchUp(1, x2, y2)
    mSleep(300)
end
-- 写入plist
function writePlist(muLu, jian, zhi)
    -- 写入键值
    local plfilename = muLu -- 设置plist路径
    local tmp = plist.read(plfilename) -- 读取plist文件内容并返回一个TABLE
    if tmp == nil then
        fi = io.open(muLu, 'w')
        if fi then
            fi:write(var.plistsTMP)
            fi:close()
            tmp = plist.read(plfilename) -- 读取plist文件内容并返回一个TABLE
        else
            dialog(muLu .. "_" .. jian .. "_" .. zhi, 0)
        end
    end
    tmp[jian] = zhi
    plist.write(plfilename, tmp) -- 将修改后的表写入PLIST文件
end
-- 写入plist
function writePlist2(muLu, jian1, jian2, zhi)
    -- 写入键值
    local plfilename = muLu -- 设置plist路径
    local tmp = plist.read(plfilename) -- 读取plist文件内容并返回一个TABLE
    if tmp == nil then
        fi = io.open(muLu, 'w')
        if fi then
            fi:write(var.plistsTMP)
            fi:close()
            tmp = plist.read(plfilename) -- 读取plist文件内容并返回一个TABLE
        else
            dialog(muLu .. "_" .. jian1 .. "_" .. jian2 .. "_" .. zhi, 0)
        end
    end
    tmp[jian1][jian2] = zhi
    plist.write(plfilename, tmp) -- 将修改后的表写入PLIST文件
end
-- 写入调试
function tiaoShi(tiaoShiNeiRong)
    local tmpXuanXiang = string.find(var.zongHeXuanXiang1, "3") -- 网络调试
    if tmpXuanXiang ~= nil then
        local tmpXuanXiang = string.find(var.zongHeXuanXiang1, "0")
        if tmpXuanXiang ~= nil then
            wLog(var.wanmaoIP,
                var.iphoneId .. " 目标1: " .. var.muBiao1 .. " 目标2: " .. var.muBiao2 .. " 目标3: " ..
                    var.muBiao3 .. "    操作:" .. tiaoShiNeiRong .. "   #" .. var.xiangMu .. "#   [DATE]")
        end
        local tmpXuanXiang = string.find(var.zongHeXuanXiang1, "1")
        if tmpXuanXiang ~= nil then
            wLog(var.wanmaoIP,
                var.iphoneId .. " 目标1: " .. var.muBiao1 .. " 目标2: " .. var.muBiao2 .. " 目标3: " ..
                    var.muBiao3 .. "    操作:" .. tiaoShiNeiRong .. "   #" .. var.xiangMu .. "#   [DATE]")
        end
    end
    local tmpXuanXiang = string.find(var.zongHeXuanXiang1, "2")
    if tmpXuanXiang ~= nil then
        -- dialog(tiaoShiNeiRong,5)
        toast("目标1: " .. var.muBiao1 .. " 目标2: " .. var.muBiao2 .. " 目标3: " .. var.muBiao3 ..
                  "                操作:" .. tiaoShiNeiRong, 1)
    end
end



-- 更改配置文件
function writeConfig(filename, number, neirong)
    local f = io.open(var.luaPath .. "/config/" .. filename, 'r')
    local tmpString = f:read("*all")
    local tmpWeiZhi = {}
    local tmpQianDuan = ""
    local tmpHouDuan = ""
    if number == 1 then
        tmpQianDuan = "ui_input::::"
        tmpWeiZhi[1] = string.find(tmpString, "###");
        tmpHouDuan = string.sub(tmpString, tmpWeiZhi[1], -1)
        f:close()
        f = io.open(var.luaPath .. "/config/" .. filename, 'w')
        -- dialog(tmpQianDuan..neirong..tmpHouDuan,0)
        f:write(tmpQianDuan .. neirong .. tmpHouDuan)
        f:close()
    end

    if number > 1 then
        for i = 1, number do
            if i == 1 then
                tmpWeiZhi[i] = string.find(tmpString, "###");
            else
                tmpWeiZhi[i] = string.find(tmpString, "###", tmpWeiZhi[i - 1] + 3);
            end

        end
        tmpQianDuan = string.sub(tmpString, 1, tmpWeiZhi[number - 1] + 2)
        tmpHouDuan = string.sub(tmpString, tmpWeiZhi[number], -1)

        f:close()
        f = io.open(var.luaPath .. "/config/" .. filename, 'w')
        f:write(tmpQianDuan .. neirong .. tmpHouDuan)
        f:close()
    end
end
-- 目标转换
function muBiaoZhuanHuan(...)
    -- tiaoShi("var.muBiao3"..var.muBiao3)
    -- mSleep(200)
    -- tiaoShi("var.muBiao2"..var.muBiao2)
    -- mSleep(200)
    -- tiaoShi("var.muBiao1"..var.muBiao1)
    -- mSleep(200)
    -- tiaoShi("var.muBiao"..var.muBiao)
    -- mSleep(200)
    -- tiaoShi("var.mb_Wu"..var.mb_Wu)
    -- mSleep(200)

    if var.muBiao3 ~= var.mb_Wu then
        var.muBiao = var.muBiao3
        timeDingShiDianRenWu = nowTime - 60 * 60
        -- tiaoShi(var.muBiao)
    elseif var.muBiao2 ~= var.mb_Wu then
        var.muBiao = var.muBiao2
        timeDingShiDianRenWu = nowTime - 60 * 60
        -- tiaoShi(var.muBiao)

    elseif var.muBiao1 ~= var.mb_Wu then
        var.muBiao = var.muBiao1
        -- tiaoShi(var.muBiao)
        -- timeDingShiDianRenWu = nowTime - 60 * 60
    else
        var.muBiao1 = var.mb_GuaJi
        var.muBiao1Con = var.mm_GuaJi
        writeConfig(var.xiangMu .. "_Set.dat", var.mbCon1, var.muBiao1Con)
        var.muBiao = var.muBiao1
        -- tiaoShi(var.muBiao)
        -- timeDingShiDianRenWu = nowTime - 60 * 60
    end
end
beforeBianLiang()
